package com.hui.auth.service.impl;

import com.hui.auth.config.shiro.SecurityUtil;
import com.hui.auth.dao.AuthMenuDao;
import com.hui.auth.entity.AuthMenu;
import com.hui.auth.entity.AuthUser;
import com.hui.auth.service.AuthMenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AuthMenuServiceImpl implements AuthMenuService {
    @Autowired
    private AuthMenuDao authMenuDao;

    @Override
    public boolean insert(AuthMenu menu) {
        return authMenuDao.insert(menu);
    }

    @Override
    public List<AuthMenu> selectAll() {
        return null;
    }

    @Override
    public boolean update(AuthMenu menu) {
        return authMenuDao.update(menu);
    }

    @Override
    public boolean updateRoleMenuByRid(Integer rid, List<Integer> mids) {
        authMenuDao.deleteByRid(Long.parseLong(rid + ""));
        authMenuDao.insertRoleMenu(rid, mids);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        if (1 == authMenuDao.delete(id)) {
            return true;
        }
        return false;
    }

    @Override
    public List<AuthMenu> selectTreeByRoleId() {
        AuthUser user = SecurityUtil.currentUser();
        String roleIds = user.getRoleIds();
        List<AuthMenu> menus = authMenuDao.selectByRoleId(roleIds, 0L);//一级菜单
        for (AuthMenu menu : menus) {
            menu.setSubMenu(authMenuDao.selectByRoleId(roleIds, menu.getId()));//子菜单
        }
        return menus;
    }

    @Override
    public List<AuthMenu> selectAllTree() {
        List<AuthMenu> menus = authMenuDao.selectChildren(0L);//一级菜单
        for (AuthMenu menu : menus) {
            menu.setSubMenu(authMenuDao.selectChildren(menu.getId()));//子菜单
        }
        return menus;
    }

}
